//
//  NWVisitorView.swift
//  微博01
//
//  Created by 牛威 on 15/12/6.
//  Copyright © 2015年 牛威. All rights reserved.
//

import UIKit
import SnapKit


class NWVisitorView: UIView {
    
    
    
    //MARK: - 抽取不同的界面显示不同的访客视图
    
    
    func setVisitorViewInfo(imageName:String?,message:String) {
        
        
        label.text = message
        
        guard let imgName = imageName else {
            
            startAnimation()
            
            return
        }
        
        iconView.hidden = true
        
        homeView.image = UIImage(named: imgName)
        
        sendSubviewToBack(maskview)
        
    }


    
    //initWithFram是UIView的指定构造函数
    //使用纯代码开发的时候使用的
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        
        setupUI()
    }
    
    //initWithCoder 使用XIB&SB开发加载的函数
    //使用SB开始的入口

    required init?(coder aDecoder: NSCoder) {
        
        //导致如果使用SB开发,调用这个视图会直接崩溃 如果当前开发是纯代码的话就可以使用这句话 不想代码的话就调用父类方法
//        fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
        
        setupUI()
        
    }
    
    

    
    
    //MARK: - 懒加载控件 
    //使用image构造函数 创建的视图就是imageview的大小
    
    lazy var iconView : UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    lazy var maskview : UIImageView = UIImageView(imageName:"visitordiscover_feed_mask_smallicon")
    
    lazy var homeView :UIImageView = UIImageView (imageName:"visitordiscover_feed_image_house")
    
    
    lazy var label : UILabel = UILabel(title:"最美的不是下雨天,而是与你一起发过微博的下午")
    
    
    lazy var regBtn : UIButton = UIButton(title: "注册", backImageName: "common_button_white_disable", color: UIColor.orangeColor())
    
    
    lazy var loginBtn : UIButton = UIButton(title: "登录", backImageName: "common_button_white_disable", color: UIColor.darkGrayColor())
    

}


extension NWVisitorView {

    /// 设置界面
    private func setupUI(){
        
        // 1 添加控件
        
        addSubview(iconView)
        
        addSubview(maskview)
        
        addSubview(homeView)
        
        addSubview(label)
        
        addSubview(regBtn)
        
        addSubview(loginBtn)
        
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
            
        }
        
        homeView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_centerX)
            make.centerY.equalTo(iconView.snp_centerY)
        }
        
        label.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        
        regBtn.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(label.snp_leading)
            make.top.equalTo(label.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            
            make.trailing.equalTo(label.snp_trailing)
            make.top.equalTo(label.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
            
        }
        
        maskview.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top)
            make.width.equalTo(self.snp_width)
            make.bottom.equalTo(regBtn.snp_bottom)
            
            backgroundColor  = UIColor(white: 237.0 / 255.0, alpha: 1)

            
        }
        

        
}
    //设置首页动画
    
    func startAnimation() {
        
        //设置动画
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.duration  = 20
        
        animation.repeatCount = MAXFLOAT
        
        //用在不断重复的动画上,当动画绑定的涂层对应的视图被销毁时,他也随之销毁
        
        animation.removedOnCompletion = false
        
        iconView.layer.addAnimation(animation, forKey: nil)
        
        
        
        
    }


    

//    func setupFrame(){
//        
//        // 2 设置自动布局
//        
//        //设置水平居中
//        
//        for v in subviews {
//            
//            v.translatesAutoresizingMaskIntoConstraints = false
//            
//        }
//        
//        //设置圆圈
//        
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        
//        
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY , relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60 ))
//        
//        //设置房子
//        
//        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        
//        
//        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterY , relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
//        
//        //设置label
//        
//        addConstraint(NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: label, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        
//        addConstraint(NSLayoutConstraint(item: label, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
//        
//        addConstraint(NSLayoutConstraint(item: label, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
//        
//        //设置regBtn
//        
//        addConstraint(NSLayoutConstraint(item: regBtn, attribute: .Left, relatedBy: .Equal, toItem: label, attribute: .Left, multiplier: 1.0, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: regBtn, attribute: .Top, relatedBy: .Equal, toItem: label, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        
//        addConstraint(NSLayoutConstraint(item: regBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
//        
//        addConstraint(NSLayoutConstraint(item: regBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
//        
//        //设置loginBtn
//        
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Right, relatedBy: .Equal, toItem: label, attribute: .Right, multiplier: 1.0, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: label, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
//        
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
//        
//        
//        //设置遮罩 VFL visiual format language 可视格式化语言
//        
//        //H水平 V垂直 |边界 [] 包装控件
//        
//        //views 字典 [名字:控件名] 字符串中表示控件的字符串
//        
//        //metrics 字典 [名字:NSNumber] 字符串中表示某一个数值
//        
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":maskview]))
//        
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[regBtn]", options: [], metrics: ["btnHeight":-36], views: ["mask":maskview,"regBtn":regBtn]))
//        
//        
//        backgroundColor  = UIColor(white: 237.0 / 255.0, alpha: 1)
//        
//        
//    }




}
